package com.rental.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "返回结果")
public class AjaxResult<T> {
    @Schema(description = "返回代码")
    private Integer code;

    @Schema(description = "返回信息")
    private String message;

    @Schema(description = "返回数据")
    private T data;

    public AjaxResult() {
    }

    protected static <T> AjaxResult<T> build(T data) {
        AjaxResult<T> ajaxResult = new AjaxResult<>();
        if (data != null) ajaxResult.setData(data);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> build(Integer code, String message, T data) {
        AjaxResult<T> ajaxResult = build(data);
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static <T> AjaxResult<T> build(T data, ResultCodeEnum resultCodeEnum) {
        AjaxResult<T> ajaxResult = build(data);
        ajaxResult.setCode(resultCodeEnum.getCode());
        ajaxResult.setMessage(resultCodeEnum.getMessage());
        return ajaxResult;
    }

    public static <T> AjaxResult<T> success(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> AjaxResult<T> success() {
        return success(null);
    }

    public static <T> AjaxResult<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }
    public static <T> AjaxResult<T> fail() {
        return fail(null);
    }
}
